package com.ychen.ruijee.controller;

import com.ychen.ruijee.bean.User;
import com.ychen.ruijee.common.R;
import com.ychen.ruijee.service.UserService;
import com.ychen.ruijee.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author ychen@gotop.cn
 * @date 2024-06-05 23:40
 */

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 移动端 用户登录
     * @param map
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<User> userLogin(@RequestBody Map map,HttpSession session){
       User user =  userService.userLogin(map,session);
       return user == null ? R.error("登录失败") : R.success(user);

    }

    /**
     * 发送验证码
     * {phone: "13212345678"} 前端传递的是 JSON  所以要用 @RequestBody 来接受
     * @param user
     * @param session
     * @return
     */
    /* 优化 将验证码存放在 redis 并设置有效时间*/
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        R<String> result = userService.sendMsg(user,session);
       return result;

    }
}
